﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using BasesWebRef = Sunboxsoft.ASMS.Client.Domain.BasesWebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;

namespace Sunboxsoft.ASMS.Client.UI
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();

            this.ctlLabelVersion.Text = string.Format("v{0}", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(4));
        }

        private void ctlBtnLogin_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
            UIContext.Current.InvokeAsync(() =>
            {
                SingleResult<BasesWebRef.Account> result = new Service.LoginService().Login(ctlEditAccount.Text.Trim(),
                    ctlEditPassword.Text.Trim());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ModuleContext.Current.AccountId = result.Data.Id;
                        ModuleContext.Current.UserName = result.Data.Name;

                        this.Cursor = Cursors.Arrow;
                        DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(result.Message);

                        this.Cursor = Cursors.Arrow;
                        this.Enabled = true;
                    }
                }));
            });
        }
    }
}
